var fileImported = document.getElementById("import");

var models;

fileImported.addEventListener("change", function(){
  models = fileImported.files[0];
  selectLoader();});

function selectLoader(){
        var reader = new FileReader();
        reader.addEventListener('load',function(evt){
            var fillName = models.name;
            var contents = evt.target.result;
            var type = fillName.split(".").pop().toLowerCase();
            // console.log(type);
            switch (type) {
                case "vtk":
                    // statements_1

                    var geometry= new THREE.VTKLoader().parse(contents);
                    geometry.sourceType = "vtk";
                    geometry.center();
                    console.log(geometry);
                    geometry.computeFaceNormals();
                    geometry.computeVertexNormals();
                    // geometry.sourceFile = models.name;
                    // console.log(geometry.sourceFile);
                    var material = new THREE.MeshLambertMaterial( { color: 0xffffff, side: THREE.DoubleSide } );
                    var mesh = new THREE.Mesh( geometry, material );
                    mesh.name = fillName.split(".")[0];
                    mesh.scale.multiplyScalar( 1000 );
                    mesh.position.set( 0, 0, 0 );
                    scene.add( mesh );


                    control.attach(mesh);
                    activeObject = mesh;
                    var gui = new dat.GUI({autoPlace: false});
                    mesh.gui = gui;
                    
                    gui.add(mesh,"name");
                    var pos = gui.addFolder('position');
                    pos.add(mesh.position,'x').listen();
                    pos.add(mesh.position,'y').listen();
                    pos.add(mesh.position,'z').listen();
                    var pos = gui.addFolder('rotation');
                    pos.add(mesh.rotation,'x').listen();
                    pos.add(mesh.rotation,'y').listen();
                    pos.add(mesh.rotation,'z').listen();
                    var pos = gui.addFolder('scale');
                    pos.add(mesh.scale,'x').listen();
                    pos.add(mesh.scale,'y').listen();
                    pos.add(mesh.scale,'z').listen();

                    propertyPanel.update();
                    materialPanel.update();
                    modifierViewer.update();
                    modifierSelectorViewer.update();
                    outliner.addOutlinerItem(mesh.name,mesh.id);
                    
                    break;
                case 'obj':
                    var result = new THREE.OBJLoader().parse( contents );
                    
                    var mesh = result.children[0];
                    console.log(mesh);
                    mesh.geometry.sourceType = "obj";
                    mesh.geometry.center();
                     mesh.geometry.computeFaceNormals();
                     mesh.geometry.computeVertexNormals();
                     mesh.position.set( 0, 0, 0 );
                    
                    mesh.name = fillName.split(".")[0];
                    // mesh.scale.multiplyScalar( 1000 );
                    mesh.position.set( 0, 0, 0 );
                    scene.add( mesh );


                    control.attach(mesh);
                    activeObject = mesh;
                    var gui = new dat.GUI({autoPlace: false});
                    mesh.gui = gui;
                    
                    gui.add(mesh,"name");
                    var pos = gui.addFolder('position');
                    pos.add(mesh.position,'x').listen();
                    pos.add(mesh.position,'y').listen();
                    pos.add(mesh.position,'z').listen();
                    var pos = gui.addFolder('rotation');
                    pos.add(mesh.rotation,'x').listen();
                    pos.add(mesh.rotation,'y').listen();
                    pos.add(mesh.rotation,'z').listen();
                    var pos = gui.addFolder('scale');
                    pos.add(mesh.scale,'x').listen();
                    pos.add(mesh.scale,'y').listen();
                    pos.add(mesh.scale,'z').listen();

                    propertyPanel.update();
                    materialPanel.update();
                    modifierViewer.update();
                    modifierSelectorViewer.update();
                    outliner.addOutlinerItem(mesh.name,mesh.id);
                    break;
                    
                default:
                    alert("不支持该格式文件，请选择 vtk、")
                    break;
               
            }
            
            // console.log(mesh.name);
            // console.log(mesh.id);
        });
        reader.readAsText(models);
}

